//
//  FQDBannerArticleFeedView.m
//  FakeQDaily
//
//  Created by mewxiv on 8/13/16.
//  Copyright © 2016 Xensius. All rights reserved.
//

#import "FQDBannerArticleFeedView.h"
#import "FQDRestModel.h"

#import "Masonry.h"
#import "UIImageView+WebCache.h"


@interface FQDBannerArticleFeedView ()

// 横幅图片
@property (nonatomic, strong) UIImageView *imageView;
// 遮罩层
@property (nonatomic, strong) UIView *maskImageView;
// 横幅标题
@property (nonatomic, strong) UILabel *titleLabel;

@end

@implementation FQDBannerArticleFeedView

#pragma mark - View configuration
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self prepareUI];
        [self prepareConstraints];
    }
    return self;
}

- (void)prepareUI
{
    _imageView = [[UIImageView alloc] init];
    _imageView.contentMode = UIViewContentModeScaleAspectFill;
    _imageView.clipsToBounds = YES;
    [self addSubview:_imageView];
    
    UIImage *maskImage = [UIImage imageNamed:@"home_feed_mask_large_normal"];
    _maskImageView = [[UIImageView alloc] initWithImage:maskImage];
    [self addSubview:_maskImageView];

    _titleLabel = [[UILabel alloc] init];
    _titleLabel.font = FQDBoldFontWithSize(22.0f);
    _titleLabel.textColor = [UIColor whiteColor];
    _titleLabel.numberOfLines = 3;
    _titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
    _titleLabel.preferredMaxLayoutWidth = 300.0f;
    [self addSubview:_titleLabel];
}

- (void)prepareConstraints
{
    WeakSelf;

    [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf);
    }];
    
    [_maskImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf.imageView);
    }];

    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        UIEdgeInsets edgeInsets = UIEdgeInsetsMake(0, 10, 0, 10);
        make.left.and.right.equalTo(weakSelf.imageView).insets(edgeInsets);
        make.bottom.equalTo(weakSelf.imageView).offset(-33);
    }];
}

#pragma mark - Getters and Setters
- (void)setBanner:(FQDFeed *)banner
{
    _banner = banner;

    FQDPost *post = banner.post;

    [self.imageView sd_setImageWithURL:[NSURL URLWithString:post.image]];
    [self.titleLabel setText:post.title];
}

@end
